package com.ling.pl.security.service;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.core.service.impl.BaseService;
import com.ling.pl.security.dao.DefaultGroupDao;
import com.ling.pl.security.model.DefaultGroup;
import com.ling.pl.security.model.DefaultGroupVO;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 */
public class BaseDefaultGroupService extends BaseService {

    public static final String BEAN_ID = "defaultGroupService";

    @Resource
    DefaultGroupDao defaultGroupDao;

    public IDao getDao() {
        return defaultGroupDao;
    }

    public List<DefaultGroupVO> convertDefaultGroupToVO(List<DefaultGroup> models) {
        List<DefaultGroupVO> result = new ArrayList<DefaultGroupVO>();
        for (DefaultGroup initiation : models) {
            DefaultGroupVO inparam = convertDefaultGroupToVO(initiation);
            result.add(inparam);
        }
        return result;
    }

    public DefaultGroupVO convertDefaultGroupToVO(DefaultGroup model) {
        DefaultGroupVO inparam = new DefaultGroupVO();
        ReflectUtils.copyProperties(model, inparam);
        return inparam;
    }

    public DefaultGroup convertDefaultGroupVOTo(DefaultGroupVO vo) {
        DefaultGroup entity = new DefaultGroup();
        ReflectUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<DefaultGroup> convertDefaultGroupVOTo(List<DefaultGroupVO> vos) {
        List<DefaultGroup> result = new ArrayList<DefaultGroup>();
        for (DefaultGroupVO vo : vos) {
            DefaultGroup entity = convertDefaultGroupVOTo(vo);
            result.add(entity);
        }
        return result;
    }

    public DaoPage findDefaultGroupVOByParams(Map params, Integer pageIndex,
                                              Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = defaultGroupDao.findDefaultGroupByParams(params, pageIndex, pageSize);
        daoPage.setResult(convertDefaultGroupToVO((List<DefaultGroup>) daoPage.getResult()));
        return daoPage;
    }

    public List<DefaultGroupVO> findDefaultGroupVOByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<DefaultGroup> temp = defaultGroupDao.findDefaultGroupByParams(params);
        return convertDefaultGroupToVO(temp);
    }

    public DaoPage findDefaultGroupByParams(Map params, Integer pageIndex,
                                            Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = defaultGroupDao.findDefaultGroupByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<DefaultGroup> findDefaultGroupByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<DefaultGroup> temp = defaultGroupDao.findDefaultGroupByParams(params);
        return temp;
    }


    public void saveDefaultGroupDataListsMap(Map<String, List<DefaultGroupVO>> dataListsMap) {
        Collection<DefaultGroupVO> deleteList = BatchUtils.to(DefaultGroupVO.class).getDeleteEntities(dataListsMap);
        Collection<DefaultGroupVO> insertList = BatchUtils.to(DefaultGroupVO.class).getInsertEntities(dataListsMap);
        Collection<DefaultGroupVO> updateList = BatchUtils.to(DefaultGroupVO.class).getModifiedEntities(dataListsMap);
        if (updateList != null && updateList.size() > 0) {
            for (DefaultGroupVO vo : updateList) {
                DefaultGroup entity = convertDefaultGroupVOTo(vo);
                defaultGroupDao.update(entity);
            }
//			defaultGroupDao.updateAll(updateList);
        }
        if (insertList != null && insertList.size() > 0) {
            for (DefaultGroupVO vo : insertList) {
                DefaultGroup entity = convertDefaultGroupVOTo(vo);
                defaultGroupDao.save(entity);
                vo.setId(entity.getId());
            }
//			defaultGroupDao.saveAll(insertList);
        }
        if (deleteList != null && deleteList.size() > 0) {
            for (DefaultGroupVO vo : deleteList) {
                DefaultGroup entity = new DefaultGroup();
                entity.setId(vo.getId());
                defaultGroupDao.remove(entity);
            }
//			defaultGroupDao.removeAll(deleteList);
        }

    }
}

